/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package me.panpf.sketch.sample.item;

import me.panpf.sketch.sample.ResourceTable;
import ohos.agp.components.*;
import ohos.app.Context;
import ohos.global.resource.RawFileEntry;
import ohos.global.resource.Resource;
import ohos.media.image.ImageSource;
import ohos.media.image.PixelMap;

import java.util.List;
import java.util.Optional;

/**
 * StaggeredImageItem
 */
public class StaggeredImageItem extends BaseItemProvider {
    private List<String> imageList;
    private Context context;
    private LayoutScatter inflater;

    public StaggeredImageItem(Context context, List<String> imageList) {
        this.imageList = imageList;
        this.context = context;
        this.inflater = LayoutScatter.getInstance(context);
    }

    @Override
    public int getCount() {
        return imageList.size();
    }

    @Override
    public String getItem(int i1) {
        return imageList.get(i1);
    }

    @Override
    public long getItemId(int i1) {
        return i1;
    }

    @Override
    public Component getComponent(int i1, Component convertView, ComponentContainer componentContainer) {
        Component view = convertView;
        final ViewHolder holder;
        if (convertView == null) {
            view = inflater.parse(ResourceTable.Layout_list_item_image_staggered, componentContainer, false);
            holder = new ViewHolder();
            holder.itemImage = (Image) view.findComponentById(ResourceTable.Id_item_image);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }

        holder.itemImage.setPixelMap(decodeResource(this.context, ResourceTable.Media_ic_launcher));
        return view;
    }

    /**
     * decodeResource
     * @param context 上下文
     *
     * @param id ResourceTable.Media_icon
     * @return PixelMap
     */
    private PixelMap decodeResource(Context context, int id) {
        try {
            String path = context.getResourceManager().getMediaPath(id);
            if (path.isEmpty()) {
                return null;
            }
            RawFileEntry assetManager = context.getResourceManager().getRawFileEntry(path);
            ImageSource.SourceOptions options = new ImageSource.SourceOptions();
            options.formatHint = "image/png";
            ImageSource.DecodingOptions decodingOptions = new ImageSource.DecodingOptions();
            Resource asset = assetManager.openRawFile();
            ImageSource source = ImageSource.create(asset, options);
            return Optional.ofNullable(source.createPixelmap(decodingOptions)).get();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    private static class ViewHolder {
        private Image itemImage;
    }
}
